package com.hspedu.socket.MyTCPFileCopy;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class Client {
    public static void main(String[] args) throws IOException {
//        String filePath = "D:\\bg.png";
        String filePath = "G:\\学习笔记\\编程\\Java后端\\JavaSE\\韩顺平 2021零基础学Java 【软件 资料 代码 笔记】\\软件.zip";
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        Socket socket = new Socket(hostAddress, 8888);//连接客户端
        if(socket!= null){
            System.out.println("已连接服务端\n服务端ip地址:" + socket.getInetAddress());
        }

        //发送图片
        //1.读取图片 filePath
        //2.通过OutPutStream 写入到数据通道中
        BufferedInputStream bi= new BufferedInputStream(new FileInputStream(filePath));
        OutputStream outputStream = socket.getOutputStream();
        StreamUtils.outputFile(bi,outputStream);
        socket.shutdownOutput();
        System.out.println("客服端:发送文件成功");


        //从服务器段读取消息
        byte[] buff = new byte[50];
        InputStream inputStream = socket.getInputStream();
        int readLen = inputStream.read(buff);
        System.out.println(new String(buff,0,readLen));

        socket.close();
        System.out.println("客户端退出");
    }
}
